import 'dart:convert';

import 'package:imperius_insights/entity/hr.dart';
import 'package:imperius_insights/portal/channels/factory.dart';
import 'package:imperius_insights/portal/channels/web.dart';
import 'package:imperius_insights/treaty/endpoint.dart';

class Service {
  Future<List<HumanResource>> queryHumansByDepartment(
      String departmentId) async {
    final channel = Channels.shared<WebChannel>(ChannelType.Web);
    final params = {
      'param': departmentId,
      'parent': '2',
      'typew': '',
    };
    final response = await channel.invoke(
        target: queryProjectsPlan,
        params: params);
    final json = jsonDecode(response);
    final nodeData = json['data'];
    final nodeEmployees = nodeData['employees'];
    final result = (nodeEmployees as List).map((e) {
      final name = e['name'];
      final sysName = e['rtx_id'];
      return HumanResource(name, sysName);
    }).toList(growable: false);
    final modified = result.reversed.toList(growable: true);
    modified.insert(0, HumanResource('所有', null));
    return modified;
  }
}